<script lang="ts">
  import { effect, reactive } from "@feng3d/reactivity";
  import { app, Todo } from "../App";

  let todos: Todo[] = [];

  let newTodoText: string = "";

  effect(() => {
    const r_app = reactive(app);

    todos = r_app.todos.map((todo) => ({ ...todo }));
  });

  function addTodo(): void {
    if (newTodoText.trim()) {
      const newTodo: Todo = {
        id: Date.now(),
        text: newTodoText.trim(),
        completed: false,
      };
      reactive(app.todos).push(newTodo);
      newTodoText = "";
    }
  }

  function handleKeyPress(event: KeyboardEvent): void {
    if (event.key === "Enter") {
      addTodo();
    }
  }
</script>

<div class="todo-list">
  <div class="todo-input">
    <input
      type="text"
      bind:value={newTodoText}
      placeholder="输入新的待办事项..."
      on:keypress={handleKeyPress}
    />
    <button on:click={addTodo} class="add-btn">添加</button>
  </div>

  <div class="todo-stats">
    <span class="stat">总计: {todos.length}</span>
    <span class="stat">已完成: {todos.filter((t) => t.completed).length}</span>
    <span class="stat">未完成: {todos.filter((t) => !t.completed).length}</span>
  </div>

  <div class="todo-items">
    {#if todos.length === 0}
      <div class="empty-state">
        <p>📝 还没有待办事项，添加一个吧！</p>
      </div>
    {:else}
      {#each todos as todo (todo.id)}
        <div class="todo-item" class:completed={todo.completed}>
          <label class="todo-checkbox">
            <input
              type="checkbox"
              checked={todo.completed}
              on:change={() => app.toggleTodo(todo.id)}
            />
            <span class="checkmark"></span>
          </label>
          <span class="todo-text">{todo.text}</span>
          <button
            class="delete-btn"
            on:click={() => app.removeTodo(todo.id)}
            title="删除任务"
          >
            🗑️
          </button>
        </div>
      {/each}
    {/if}
  </div>
</div>

<style>
  .todo-list {
    background: rgba(255, 255, 255, 0.95);
    border-radius: 15px;
    padding: 20px;
    border: 1px solid rgba(52, 152, 219, 0.2);
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
  }

  .todo-input {
    display: flex;
    gap: 10px;
    margin-bottom: 20px;
  }

  .todo-input input {
    flex: 1;
    padding: 12px;
    border: 2px solid #ecf0f1;
    border-radius: 8px;
    background: white;
    color: #2c3e50;
    font-size: 1rem;
    transition: border-color 0.3s ease;
  }

  .todo-input input:focus {
    outline: none;
    border-color: #3498db;
    box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.1);
  }

  .todo-input input::placeholder {
    color: #95a5a6;
  }

  .add-btn {
    padding: 12px 20px;
    background: linear-gradient(45deg, #2ecc71, #27ae60);
    color: white;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-weight: bold;
    transition: all 0.3s ease;
    box-shadow: 0 4px 12px rgba(46, 204, 113, 0.3);
  }

  .add-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(46, 204, 113, 0.4);
  }

  .todo-stats {
    display: flex;
    justify-content: space-around;
    margin-bottom: 20px;
    padding: 15px;
    background: rgba(52, 152, 219, 0.1);
    border-radius: 10px;
    border: 1px solid rgba(52, 152, 219, 0.2);
  }

  .stat {
    color: #2c3e50;
    font-weight: 500;
  }

  .todo-items {
    max-height: 300px;
    overflow-y: auto;
  }

  .empty-state {
    text-align: center;
    padding: 40px 20px;
    color: #7f8c8d;
  }

  .empty-state p {
    margin: 0;
    font-size: 1.1rem;
  }

  .todo-item {
    display: flex;
    align-items: center;
    padding: 12px;
    margin: 8px 0;
    background: rgba(255, 255, 255, 0.9);
    border-radius: 10px;
    transition: all 0.3s ease;
    border: 1px solid #ecf0f1;
  }

  .todo-item:hover {
    background: rgba(52, 152, 219, 0.1);
    transform: translateX(5px);
  }

  .todo-item.completed {
    opacity: 0.7;
    background: rgba(46, 204, 113, 0.1);
  }

  .todo-checkbox {
    position: relative;
    display: inline-block;
    width: 20px;
    height: 20px;
    margin-right: 15px;
    cursor: pointer;
  }

  .todo-checkbox input {
    opacity: 0;
    width: 0;
    height: 0;
  }

  .checkmark {
    position: absolute;
    top: 0;
    left: 0;
    width: 20px;
    height: 20px;
    background: white;
    border: 2px solid #bdc3c7;
    border-radius: 4px;
    transition: all 0.3s ease;
  }

  .todo-checkbox input:checked ~ .checkmark {
    background: #2ecc71;
    border-color: #2ecc71;
  }

  .checkmark:after {
    content: "";
    position: absolute;
    display: none;
    left: 6px;
    top: 2px;
    width: 5px;
    height: 10px;
    border: solid white;
    border-width: 0 2px 2px 0;
    transform: rotate(45deg);
  }

  .todo-checkbox input:checked ~ .checkmark:after {
    display: block;
  }

  .todo-text {
    flex: 1;
    color: #2c3e50;
    font-size: 1rem;
    transition: all 0.3s ease;
  }

  .todo-item.completed .todo-text {
    text-decoration: line-through;
    color: #7f8c8d;
  }

  .delete-btn {
    background: none;
    border: none;
    font-size: 1.2rem;
    cursor: pointer;
    padding: 5px;
    border-radius: 5px;
    transition: all 0.3s ease;
    opacity: 0.7;
  }

  .delete-btn:hover {
    opacity: 1;
    background: rgba(231, 76, 60, 0.2);
    transform: scale(1.1);
  }

  /* 滚动条样式 */
  .todo-items::-webkit-scrollbar {
    width: 6px;
  }

  .todo-items::-webkit-scrollbar-track {
    background: rgba(52, 152, 219, 0.1);
    border-radius: 3px;
  }

  .todo-items::-webkit-scrollbar-thumb {
    background: rgba(52, 152, 219, 0.3);
    border-radius: 3px;
  }

  .todo-items::-webkit-scrollbar-thumb:hover {
    background: rgba(52, 152, 219, 0.5);
  }

  /* 暗色主题 */
  :global(body.dark-mode) .todo-list {
    background: rgba(52, 73, 94, 0.95);
    border-color: rgba(52, 152, 219, 0.3);
  }

  :global(body.dark-mode) .todo-input input {
    background: #34495e;
    border-color: #2c3e50;
    color: #ecf0f1;
  }

  :global(body.dark-mode) .todo-input input:focus {
    border-color: #3498db;
  }

  :global(body.dark-mode) .todo-input input::placeholder {
    color: #7f8c8d;
  }

  :global(body.dark-mode) .todo-stats {
    background: rgba(52, 152, 219, 0.2);
    border-color: rgba(52, 152, 219, 0.3);
  }

  :global(body.dark-mode) .stat {
    color: #ecf0f1;
  }

  :global(body.dark-mode) .empty-state {
    color: #bdc3c7;
  }

  :global(body.dark-mode) .todo-item {
    background: rgba(52, 73, 94, 0.9);
    border-color: #34495e;
  }

  :global(body.dark-mode) .todo-item:hover {
    background: rgba(52, 152, 219, 0.2);
  }

  :global(body.dark-mode) .todo-item.completed {
    background: rgba(46, 204, 113, 0.2);
  }

  :global(body.dark-mode) .checkmark {
    background: #34495e;
    border-color: #2c3e50;
  }

  :global(body.dark-mode) .todo-text {
    color: #ecf0f1;
  }

  :global(body.dark-mode) .todo-item.completed .todo-text {
    color: #95a5a6;
  }

  :global(body.dark-mode) .todo-items::-webkit-scrollbar-track {
    background: rgba(52, 152, 219, 0.2);
  }

  :global(body.dark-mode) .todo-items::-webkit-scrollbar-thumb {
    background: rgba(52, 152, 219, 0.4);
  }

  :global(body.dark-mode) .todo-items::-webkit-scrollbar-thumb:hover {
    background: rgba(52, 152, 219, 0.6);
  }
</style>
